package com.csc.banking.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import com.csc.banking.model.Balance;
import com.csc.banking.model.Customer;

@Component
public class BalanceDao extends GenaricDao 
{
	@Override
	Class<?> getEntityClass() 
	{
		return Balance.class;
	}
	
	public Balance getBalance(String accountNumber)
	{
		TypedQuery<Balance> query = em.createQuery(
	            "SELECT g FROM Balance g WHERE g.accountNumber=:accountNumber", Balance.class).setParameter("accountNumber", accountNumber);
		try
		{
			return query.getSingleResult();
		}
		catch(NoResultException e)
		{
			return null;
		}
		/*
		catch(NonUniqueResultException e)
		{
			throw e;
		}
		catch(QueryTimeoutException e)
		{
			throw e;
		}
		*/
	}
	
	public Balance getDepositAccount(Customer customer)
	{
		TypedQuery<Balance> query = em.createQuery(
	            "SELECT g FROM Balance g WHERE g.customer=:customer AND g.periodType.periodType=:Deposit", Balance.class).setParameter("customer", customer).setParameter("Deposit", "Deposit");
		try
		{
			return query.getSingleResult();
		}
		catch(NoResultException e)
		{
			return null;
		}
		/*
		catch(NonUniqueResultException e)
		{
			throw e;
		}*/
	}
	
	public List<Balance> getBalances(Customer customer)
	{
		TypedQuery<Balance> query = em.createQuery(
	            "SELECT g FROM Balance g WHERE g.customer=:customer", Balance.class).setParameter("customer", customer);
		try
		{
			return query.getResultList();
		}
		catch(NoResultException e)
		{
			return new ArrayList<Balance>(0);
		}
	}
	
	
}
